import { Component, OnInit } from "@angular/core";
import { ReplaySubject } from "rxjs/ReplaySubject";
import { Subject } from "rxjs/Subject";
import * as moment from "moment";

const ENTRUST = 0;
const ENTRUST_HIS = 1;
const BUSINESS_HIS = 2;

export interface QueryDate {
  start: string;
  end: string;
}

@Component({
  // moduleId: module.id,
  selector: "entrust-business-combo",
  templateUrl: "entrust-business-combo.component.html",
  styleUrls: [
    "entrust-business-combo.component.less"
  ]
})
export class EntrustBusinessComboComponent implements OnInit {
  dateSource = new ReplaySubject(1);
  date: QueryDate = {
    start: null,
    end: null
  };
  tabList = [
    {
      text: "当日委托单",
      key: ENTRUST
    },
    {
      text: "历史委托单",
      key: ENTRUST_HIS
    },
    {
      text: "交割单",
      key: BUSINESS_HIS
    }
  ];
  constructor() { }

  ngOnInit() {
    // this.dateSource.publishReplay(1).connect();
  }

  setDate(date: QueryDate) {
    console.debug("Set Query Date: ", date, EntrustBusinessComboComponent.convertDate(date));
    this.dateSource.next(
      EntrustBusinessComboComponent.convertDate(date)
    );
  }

  static convertDate(date: QueryDate) {
    return {
      start: moment(date.start).format("YYYYMMDD"),
      end: moment(date.end).format("YYYYMMDD")
    };
  }

  dateOverflow(date) {
    // console.debug("Now Date: ", date);
    return moment(date.end).isSameOrAfter(moment(date.start).add(30, "d"));
  }

  invalidDate(date) {
    return (!date.start || !date.end || moment(date.start).isAfter(date.end));
  }

  dateMisMatch(date) {
    return moment(date.start).isAfter(date.end);
  }

}